package com.crm.system.controller.csd.customerManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.common.util.JSONUtility;
import com.crm.model.qo.csd.customerManager.ContactMaintainQO;
import com.crm.model.qo.csd.customerManager.ContactMaintainUpdateQO;
import com.crm.model.vo.csd.customerManager.ContactMaintainVO;
import com.crm.system.service.csd.customerManager.ContactMaintainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * (客服部)联系人管理
 * @author yincuimei
 * @Date 2021/10/25 16:08
 */
@RestController
@RequestMapping("/csd/contactMaintain")
@Slf4j
public class ContactMaintainController {
    @Autowired
    private ContactMaintainService contactMaintainService;

    /**
     * (客服部)联系人管理-》联系人维护列表
     * @author yincuimei
     * @return
     */
    @PostMapping("/selectContactMaintainList")
    public DataResponse selectContactMaintainList(@RequestParam(value = "pageIndex") Integer pageIndex,//页码
                                           @RequestParam(value = "pageSize") Integer pageSize,//分页大小
                                           @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                           @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                           ContactMaintainQO contactMaintainQO//查询条件
    ){
        try{
            DhPageInfo<ContactMaintainVO> contactMaintainPageInfo = contactMaintainService.selectContactMaintainVOList(contactMaintainQO,pageIndex,pageSize,sortName,sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),contactMaintainPageInfo);
        }catch(Exception e){
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 联系人维护-》审核
     * @author yincuimei
     * @return
     */
    @PostMapping("/updateExamineByIdsBatch")
    public DataResponse updateExamineByIdsBatch( @RequestParam(value="contactIds") List<String> contactIds,
                                                 @RequestParam(value="examineInfo")Integer examineInfo){
        if(contactIds != null && contactIds.size() > 0){
            JSONObject jsonObject = contactMaintainService.updateExamineByIdsBatch(contactIds,examineInfo);
            String code = jsonObject.getString("code");
            if(jsonObject != null){
                if(!StringUtils.isBlank(code)){//审核成功
                    if("000000".equals(code)){
                        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject.getString("message"));
                    }else if("000001".equals(code)){//未选中审核列
                        return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode(),jsonObject.getString("message"));
                    }
                }
            }
        }else{
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode(),"请选中要审核的列");
        }
        return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
//        if(!StringUtils.isBlank(contactIds)){
//            List<String> contactList = JSONUtility.toObject(contactIds);
//            JSONObject jsonObject = contactMaintainService.updateExamineByIdsBatch(contactList,examineInfo);
//            String code = jsonObject.getString("code");
//            if(jsonObject != null){
//                if(!StringUtils.isBlank(code)){//审核成功
//                    if("000000".equals(code)){
//                        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject.getString("message"));
//                    }else if("000001".equals(code)){//未选中审核列
//                        return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode(),jsonObject.getString("message"));
//                    }
//                }
//            }
//        }else{
//            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode(),"请选中要审核的列");
//        }
//        return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
    }

    /**
     * 联系人维护-》修改
     * @param contactMaintainUpdateQO
     * @author yincuimei
     * @return
     */
    @PostMapping("/updateContactByContactId")
    public DataResponse updateContactByContactId(@RequestBody ContactMaintainUpdateQO contactMaintainUpdateQO){
        JSONObject jsonObject = contactMaintainService.updateContactByContactId(contactMaintainUpdateQO);
        if(jsonObject != null){
            if(jsonObject.containsKey("RP0000")){//联系方式重复
                return new DhResponse("RP0000",jsonObject.getString("RP0000"));
            }
            if(jsonObject.containsKey("-1")){//服务器错误
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode(),jsonObject.getString("-1"));
            }
            if(jsonObject.containsKey("000000")){//修改成功
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject.getString("000000"));
            }
        }
        return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
    }

    /**
     * 联系人维护-》删除(逻辑删除)
     * @describe
     *     1.企业联系人删除(lxr_contact、lxr_contact_information、chs_customer_contact_person)
     *      个人联系人删除(lxr_contact、lxr_contact_information、chs_customer)
     *      备注(个人联系人删除chs_customer是因为原先系统逻辑新增个人联系人会往客户表添加数据)
     *    2.添加到工作日志表(csd_work_log)记录此次删除操作
     * @author yincuimei
     * @return
     */
    @PostMapping("/deleteContactByContactId")
    public DataResponse deleteContactByContactId(@RequestParam(value="contactId") String contactId,//联系人ID
                                                 @RequestParam(value="contactName",required = false) String contactName,//联系人名称
                                                 @RequestParam(value="customerId",required = false) String customerId, //客户ID
                                                 @RequestParam(value="contactNature",required = false) Integer contactNature)//联系人性质
    {
        try{
            Boolean result = contactMaintainService.deleteContactByContactId(contactId,contactName,customerId,contactNature);
            if(result){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }catch(Exception e){
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 联系人维护-》导出
     * @param response
     * @param contactMaintainQO
     * @author yincuimei
     */
    @GetMapping(value="/exportContactMaintain")
    public void exportContactMaintain(HttpServletResponse response, ContactMaintainQO contactMaintainQO){
        //设置类型
        response.setContentType("application/vnd.ms-excel");
        //设置编码
        response.setCharacterEncoding("UTF-8");
        try{
            contactMaintainService.exportContactMaintain(response,contactMaintainQO);
        }catch (Exception e){
            log.error("contactMaintain exportContactMaintain error:", e);
        }
    }
}
